Mapping and Visiting in Functional and Object-Oriented Programming

نویسندگان

  • Kurt Nørmark
  • Bent Thomsen
  • Lone Leth Thomsen
چکیده

class Exp{public abstract T Accept(Visitor v, D x);}class PlusExp : Exp {private Exp e1, e2;public PlusExp(Exp a, Exp b){e1 = a;e2 = b;}public Exp LeftOperand{get {return e1;}}public Exp RightOperand{get {return e2;}}public override T Accept(Visitor v, D x){return v.Visit(this, x);}} class TimesExp : Exp {private Exp e1, e2; public TimesExp(Exp a, Exp b){e1 = a;e2 = b;} public Exp LeftOperand{get {return e1;}}public Exp RightOperand{get {return e2;}} public override T Accept(Visitor v, D x){return v.Visit(this, x);}} class Identifier : Exp {private string name; public Identifier(string s) {name = s;}public string Name{get {return name;} }public override T Accept(Visitor v, D x){return v.Visit(this, x);}}class Literal : Exp {private string litString; public string LiteralString{get {return litString;}}public Literal(string s) {litString = s; } 98JOURNAL OF OBJECT TECHNOLOGYVOL 7, NO. 7 B THE VISITOR DESIGN PATTERN public override T Accept(Visitor v, D x){return v.Visit(this, x);}} class Environment: Dictionary{}; class Interpreter: Visitor {public int Visit(PlusExp e, Environment env){return (e.LeftOperand.Accept(this, env) + e.RightOperand.Accept(this, env));}public int Visit(TimesExp e, Environment env){return (e.LeftOperand.Accept(this, env) * e.RightOperand.Accept(this, env));}public int Visit(Identifier e, Environment env){return env[e.Name];} public int Visit(Literal e, Environment env){return Int32.Parse(e.LiteralString);}} class ReversePolishConverter: Visitor { private const string SPACE = " "; public string Visit(PlusExp e, None x){return e.LeftOperand.Accept(this, x) + SPACE + e.RightOperand.Accept(this, x) +SPACE + "+";}public string Visit(TimesExp e, None x){return e.LeftOperand.Accept(this, x) + SPACE + e.RightOperand.Accept(this, x) +SPACE + "*";}public string Visit(Identifier e, None x){return e.Name;} public string Visit(Literal e, None x){return e.LiteralString;}} class None {};class Program { VOL 7, NO. 7JOURNAL OF OBJECT TECHNOLOGY99 MAPPING AND VISITING IN FUNCTIONAL AND OBJECT-ORIENTED PROGRAMMING public static void Main() {TimesExp ast = new TimesExp(new PlusExp (new Literal("1"), new Identifier("x")),new Literal("2")); // (1 + x) * 2Environment env = new Environment(); env.Add("x",9);Interpreter interpreter = new Interpreter();ReversePolishConverter reversPolishConverter = new ReversePolishConverter();None nothing = new None();Console.WriteLine(interpreter.Visit(ast, env));Console.WriteLine(reversPolishConverter.Visit(ast, nothing));}}} C DOUBLE DISPATCH VISITING IN CLOS ;;; Visitor in CLOS Common Lisp Object System. ;; Expression Classes (defclass Expression () ()) (defclass PlusExpression (Expression)((a1 :initarg :firstAddend :accessor firstAddend)(a2 :initarg :secondAddend :accessor secondAddend))) (defclass MinusExpression (Expression)((m :initarg :minuend :accessor minuend)(s :initarg :subtrahend :accessor subtrahend))) (defclass TimesExpression (Expression)((f1 :initarg :firstFactor :accessor firstFactor)(f2 :initarg :secondFactor :accessor secondFactor))) (defclass DivideExpression (Expression)((dividend :initarg :dividend :accessor dividend)(divisor :initarg :divisor :accessor divisor)))(defclass Identifier (Expression)((name :initarg :name :accessor name))) (defclass IntegerLiteral (Expression)((literal :initarg :literal :accessor literal))) ;; Visitor classes(defclass Visitor () ()) 100JOURNAL OF OBJECT TECHNOLOGYVOL 7, NO. 7 C DOUBLE DISPATCH VISITING IN CLOS (defclass Interpreter (Visitor) ())(defclass ReversePolish (Visitor) ()) ;; Visit multi methods.;; Notice that the visit method specializes on both an Expression;; and a (kind of) of visitor. (defmethod visit ((e PlusExpression) (v Interpreter) &optional; extra)(+ (visit (firstAddend e) v extra) (visit (secondAddend e) v extra)))(defmethod visit ((e MinusExpression) (v Interpreter) &optional; extra)((visit (minuend e) v extra) (visit (subtrahend e) v extra))) (defmethod visit ((e TimesExpression) (v Interpreter) &optional; extra)(* (visit (firstFactor e) v extra) (visit (secondFactor e) v extra))) (defmethod visit ((e DivideExpression) (v Interpreter) &optional; extra)(/ (visit (dividend e) v extra) (visit (divisor e) v extra))) (defmethod visit ((e Identifier) (v Interpreter) &optional; extra)(lookup-identifier extra (name e))) (defmethod visit ((e IntegerLiteral) (v Interpreter) &optional; extra)(convert-string-to-integer (literal e))) (defmethod visit ((e PlusExpression) (v ReversePolish) &optional; extra)(concatenate ’string (visit (firstAddend e) v extra) " "(visit (SecondAddend e) v extra) " " "+")) (defmethod visit ((e MinusExpression) (v ReversePolish) &optional; extra)(concatenate ’string (visit (minuend e) v extra) " "(visit (subtrahend e) v extra) " " "-")) (defmethod visit ((e TimesExpression) (v ReversePolish) &optional; extra)(concatenate ’string (visit (firstFactor e) v extra) " "(visit (SecondFactor e) v extra) " " "*"))(defmethod visit ((e DivideExpression) (v ReversePolish) &optional; extra)(concatenate ’string (visit (dividend e) v extra) " "(visit (divisor e) v extra) " " "/")) (defmethod visit ((e Identifier) (v ReversePolish) &optional; extra)(name e)) (defmethod visit ((e IntegerLiteral) (v ReversePolish) &optional; extra)(literal e)) VOL 7, NO. 7JOURNAL OF OBJECT TECHNOLOGY101 MAPPING AND VISITING IN FUNCTIONAL AND OBJECT-ORIENTED PROGRAMMING ;; Auxiliary stuff.(defun convert-string-to-integer (str &optional; (radix 10))"Given a digit string and optional radix, return an integer."; Details not relevant for this paper)(defun lookup-identifier (env name)(cdr (assoc name env :test (function equal)))) ;; Some sample visiting:(defun main ()(let* ((expr1 (make-instance ’PlusExpression:firstAddend (make-instance ’IntegerLiteral :literal "3"):secondAddend (make-instance ’IntegerLiteral :literal "2")))(expr2 (make-instance ’TimesExpression:firstFactor (make-instance ’IntegerLiteral :literal "3"):secondFactor (make-instance ’Identifier :name "var"))) (expr3 (make-instance ’MinusExpression:minuend expr1:subtrahend expr2)) (interpretation (make-instance ’Interpreter))(reverse-polish (make-instance ’ReversePolish))(env ’(("x" . 5) ("var" . 7))))(list (visit expr1 interpretation env)(visit expr2 interpretation env)(visit expr3 interpretation env) (visit expr1 reverse-polish)(visit expr2 reverse-polish)(visit expr3 reverse-polish))))

برای دانلود رایگان متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Analysis Accruing of Sentinel 2A Image’s Classification Methods Based on Object Base and Pixel Base in Flood Area Zoning of Taleqan River

Flood zonation mapping is one of the priorities for the soil and water management, which Remote Sensing (RS) capabilities are very applicable to this issue. The main objective of this research was study of accuracy of the Object oriented and Pixel based methods for flood zonation mapping in the Taleghan River basin. Therefore, the Sentinel 2A satellite image of the study area classified using s...

متن کامل

Declarative Semantics in Object-Oriented Software Development - A Taxonomy and Survey

One of the modern paradigms to develop an application is object oriented analysis and design. In this paradigm, there are several objects and each object plays some specific roles in applications. In an application, we must distinguish between procedural semantics and declarative semantics for their implementation in a specific programming language. For the procedural semantics, we can write a ...

متن کامل

Modelsaz: An Object-Oriented Computer-Aided Modeling Environment

Modeling and simulation of processing plants are widely used in industry. Construction of a mathematical model for a plant is a time-consuming and error-prone task. In light of extensive advancements in computer science (both hardware and software), computers are becoming a necessary instrument in industrial activities. Many software tools for modeling, simulation and optimization of proces...

متن کامل

Modeling and Evaluation of Stochastic Discrete-Event Systems with RayLang Formalism

In recent years, formal methods have been used as an important tool for performance evaluation and verification of a wide range of systems. In the view points of engineers and practitioners, however, there are still some major difficulties in using formal methods. In this paper, we introduce a new formal modeling language to fill the gaps between object-oriented programming languages (OOPLs) us...

متن کامل

Modeling and Evaluation of Stochastic Discrete-Event Systems with RayLang Formalism

In recent years, formal methods have been used as an important tool for performance evaluation and verification of a wide range of systems. In the view points of engineers and practitioners, however, there are still some major difficulties in using formal methods. In this paper, we introduce a new formal modeling language to fill the gaps between object-oriented programming languages (OOPLs) us...

متن کامل

Change Detection Gamasiab River Margins in Kermanshah by Comparison Pixel Base and Object Orientd Algorithms

Introduction Land use reflects the interactive characteristics of humans and the environment and describes how human exploitation works for one or more targets on the ground. Land use is usually defined on the basis of human use of the land, with an emphasis on the functional role of land in economic activities. Land use, which is associated with human activity, is undergoing change over time....

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

عنوان ژورنال:
  • Journal of Object Technology

دوره 7  شماره 

صفحات  -

تاریخ انتشار 2008